package com.shou.zhnfyxt.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shou.zhnfyxt.entity.R;
import com.shou.zhnfyxt.entity.User;
import com.shou.zhnfyxt.entity.UserDetailUserinfo;
import com.shou.zhnfyxt.mapper.UserDetailUserinfoMapper;
import com.shou.zhnfyxt.mapper.UserMapper;
import com.shou.zhnfyxt.service.UserDetailUserinfoService;
import com.shou.zhnfyxt.service.UserService;
import com.shou.zhnfyxt.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author JCY
 * @since 2022-12-05
 */
@RestController
@RequestMapping("/user/userDetailUserinfo")
public class UserDetailUserinfoController {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;
    @Autowired
    private UserDetailUserinfoService userDetailUserinfoService;

    @Autowired
    private UserDetailUserinfoMapper userDetailUserinfoMapper;

    @PostMapping("/userDetailStepTwo")
    public R<UserDetailUserinfo> getUserDetailUserinfo(@RequestBody UserDetailUserinfo userDetailUserinfo){
        System.out.println(userDetailUserinfo);
        String username = userDetailUserinfo.getUserId();
        userDetailUserinfoService.save(userDetailUserinfo);
        User user = userMapper.selectById(username);
        user.setStatus(String.valueOf(2));
        userService.updateById(user);
        return R.success(userDetailUserinfo);
    }
    @PostMapping("/getuserinfo")
    public R<UserDetailUserinfo> getUserDetailUserinfo(@RequestHeader String token){
        Claims claims = JwtUtil.parseJWT(token);
        String userId = claims.getSubject();
        System.out.println(userId);
        QueryWrapper<UserDetailUserinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id" , userId);
        UserDetailUserinfo userDetailUserinfo = userDetailUserinfoMapper.selectOne(wrapper);
        return R.success(userDetailUserinfo);
    }
    @GetMapping("getUserInfo")
    public R<UserDetailUserinfo> getUserInfo(@RequestHeader String token){
        Claims claims = JwtUtil.parseJWT(token);
        String userId = claims.getSubject();
        QueryWrapper<UserDetailUserinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        UserDetailUserinfo userDetailUserinfo = userDetailUserinfoMapper.selectOne(queryWrapper);
        return R.success(userDetailUserinfo);
    }

    @PostMapping("updateUserInfo")
    public R<String> updateUserInfo(@RequestBody UserDetailUserinfo userDetailUserinfo){
        QueryWrapper<UserDetailUserinfo> queryWrapper = new QueryWrapper<>();
        String userId = userDetailUserinfo.getUserId();
        queryWrapper.eq("user_id",userId);
        userDetailUserinfoMapper.update(userDetailUserinfo,queryWrapper);
        return R.success("success");

    }
}

